package com.geeklan.smart.widget.chart;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;

import java.util.ArrayList;
import java.util.List;

public class CustomBarChart extends BarChart {

    public CustomBarChart(Context context) {
        super(context);
    }

    public CustomBarChart(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomBarChart(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void init() {
        super.init();
        mRenderer = new SuddleBarChartRenderer(this, mAnimator, mViewPortHandler);

        updateCommonUI();
    }

    private void updateCommonUI(){
        // 不显示图例
        getLegend().setEnabled(false);
        // 不显示描述
        getDescription().setEnabled(false);
        // 不绘制网格
        setDrawGridBackground(false);
        setScaleYEnabled(false);
        setScaleXEnabled(false);
        setScaleEnabled(false);
        // 设置左y轴
        YAxis yAxis = getAxisLeft();
        // 设置y-label显示在图表外
        yAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
        // Y轴从0开始，不然会上移一点
        yAxis.setAxisMinimum(0f);
        // 设置y轴不画线
        yAxis.setDrawGridLines(false);
        // 不显示右y轴
        YAxis rightAxis = getAxisRight();
        rightAxis.setEnabled(false);
    }

    private void updateChartUI() {
        String[] labels = {"Q1", "Q2", "Q3", "Q4"};
        XAxis xAxis = getXAxis();
        // 设置自定义的ValueFormatter
        xAxis.setValueFormatter(new ValueFormatter() {
            @Override
            public String getFormattedValue(float value) {
                int index = (int) value;
                // 注意不要数组越界
                if (index >= 0 && index < labels.length) {
                    return labels[index];
                }
                return "";
            }
        });
        // 设置x轴显示在下方
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        // 设置x轴不画线
        xAxis.setDrawGridLines(false);
        // 设置x轴标签从0开始
        xAxis.setAxisMinimum(0);
        // 设置x轴显示的最大标签数量
        xAxis.setAxisMaximum(labels.length);
        // x轴标签居中
        xAxis.setCenterAxisLabels(true);
        // 接下来这段代码尤其重要，网上几乎99%的资料说的都是错的，这里详解一下
        // 1、要想标签跟group中间对齐，必须保证：(barWidth + barSpace) * group + groupSpace = granularity
        // 2、granularity < 1，则右边会有空余；
        // 3、granularity = 1，则刚好在一个屏幕显示开；
        // 4、granularity > 1，则一个屏幕显示不开；
        float barSpace = 0.025f;
        float groupSpace = 0.1f;
        float barWidth = 0.2f;
        float granularity = (barWidth + barSpace) * labels.length + groupSpace;
        xAxis.setGranularity(granularity);
        setScaleX(granularity);
        getBarData().setBarWidth(barWidth);
        groupBars(0, groupSpace, barSpace);
    }

    public void setChartData() {

        List<BarEntry> barEntries1 = new ArrayList<>();
        barEntries1.add(new BarEntry(0, 1500));
        barEntries1.add(new BarEntry(1, 1400));
        barEntries1.add(new BarEntry(2, 800));
        barEntries1.add(new BarEntry(3, 2000));
        BarDataSet barDataSet1 = new BarDataSet(barEntries1, "Q1");

        List<BarEntry> barEntries2 = new ArrayList<>();
        barEntries2.add(new BarEntry(0, 1000));
        barEntries2.add(new BarEntry(1, 1200));
        barEntries2.add(new BarEntry(2, 1500));
        barEntries2.add(new BarEntry(3, 900));
        BarDataSet barDataSet2 = new BarDataSet(barEntries2, "Q2");

        List<BarEntry> barEntries3 = new ArrayList<>();
        barEntries3.add(new BarEntry(0, 900));
        barEntries3.add(new BarEntry(1, 1000));
        barEntries3.add(new BarEntry(2, 1200));
        barEntries3.add(new BarEntry(3, 1500));
        BarDataSet barDataSet3 = new BarDataSet(barEntries3, "Q3");

        List<BarEntry> barEntries4 = new ArrayList<>();
        barEntries4.add(new BarEntry(0, 1400));
        barEntries4.add(new BarEntry(1, 800));
        barEntries4.add(new BarEntry(2, 1000));
        barEntries4.add(new BarEntry(3, 500));
        BarDataSet barDataSet4 = new BarDataSet(barEntries4, "Q4");

        List<Integer> colors = new ArrayList<>();
        colors.add(Color.GREEN);
        colors.add(Color.BLUE);
        colors.add(Color.RED);
        colors.add(Color.YELLOW);

        barDataSet1.setColor(colors.get(0));
        barDataSet2.setColor(colors.get(1));
        barDataSet3.setColor(colors.get(2));
        barDataSet4.setColor(colors.get(3));

        List<IBarDataSet> barDataSets = new ArrayList<>();
        barDataSets.add(barDataSet1);
        barDataSets.add(barDataSet2);
        barDataSets.add(barDataSet3);
        barDataSets.add(barDataSet4);

        BarData ba = new BarData(barDataSets);
        setData(ba);
    }
}
